<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者id" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员等级关联表" prop="membershipLevelId">
        <el-input
          v-model="queryParams.membershipLevelId"
          placeholder="请输入会员等级关联表"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病历id" prop="medicalRecordId">
        <el-input
          v-model="queryParams.medicalRecordId"
          placeholder="请输入病历id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民族id" prop="ethnicId">
        <el-input
          v-model="queryParams.ethnicId"
          placeholder="请输入民族id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职业id" prop="occupationId">
        <el-input
          v-model="queryParams.occupationId"
          placeholder="请输入职业id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体格信息id" prop="physicalInfoId">
        <el-input
          v-model="queryParams.physicalInfoId"
          placeholder="请输入体格信息id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方信息id" prop="prescriptionInfoId">
        <el-input
          v-model="queryParams.prescriptionInfoId"
          placeholder="请输入处方信息id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否住院" prop="isHospitalized">
        <el-input
          v-model="queryParams.isHospitalized"
          placeholder="请输入是否住院"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="床位id" prop="bedId">
        <el-input
          v-model="queryParams.bedId"
          placeholder="请输入床位id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊卡号" prop="membershipCardNumber">
        <el-input
          v-model="queryParams.membershipCardNumber"
          placeholder="请输入就诊卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="婚姻状态" prop="maritalStatus">
        <el-select v-model="queryParams.maritalStatus" placeholder="请选择婚姻状态" clearable>
          <el-option
            v-for="dict in patient_marital_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ext4">
        <el-input
          v-model="queryParams.ext4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ext5">
        <el-input
          v-model="queryParams.ext5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['registration:patient:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['registration:patient:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['registration:patient:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['registration:patient:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="患者id" align="center" prop="patientId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="会员等级关联表" align="center" prop="membershipLevelId" />
      <el-table-column label="病历id" align="center" prop="medicalRecordId" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="身份证号" align="center" prop="idCardNumber" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="民族id" align="center" prop="ethnicId">
        <template #default="scope">
          <dict-tag :options="sys_minzu" :value="scope.row.ethnicId"/>
        </template>
      </el-table-column>
      <el-table-column label="职业id" align="center" prop="occupationId">
        <template #default="scope">
          <dict-tag :options="sys_zhiye" :value="scope.row.occupationId"/>
        </template>
      </el-table-column>
      <el-table-column label="体格信息id" align="center" prop="physicalInfoId" />
      <el-table-column label="处方信息id" align="center" prop="prescriptionInfoId" />
      <el-table-column label="是否住院" align="center" prop="isHospitalized" />
      <el-table-column label="床位id" align="center" prop="bedId" />
      <el-table-column label="接诊状态" align="center" prop="receptionStatus" />
      <el-table-column label="就诊卡号" align="center" prop="membershipCardNumber" />
      <el-table-column label="婚姻状态" align="center" prop="maritalStatus">
        <template #default="scope">
          <dict-tag :options="patient_marital_status" :value="scope.row.maritalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="扩展字段3" align="center" prop="ext3" />
      <el-table-column label="扩展字段4" align="center" prop="ext4" />
      <el-table-column label="扩展字段5" align="center" prop="ext5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['registration:patient:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['registration:patient:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="patientRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="会员等级关联表" prop="membershipLevelId">
          <el-input v-model="form.membershipLevelId" placeholder="请输入会员等级关联表" />
        </el-form-item>
        <el-form-item label="病历id" prop="medicalRecordId">
          <el-input v-model="form.medicalRecordId" placeholder="请输入病历id" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="民族id" prop="ethnicId">
          <el-input v-model="form.ethnicId" placeholder="请输入民族id" />
        </el-form-item>
        <el-form-item label="职业id" prop="occupationId">
          <el-input v-model="form.occupationId" placeholder="请输入职业id" />
        </el-form-item>
        <el-form-item label="体格信息id" prop="physicalInfoId">
          <el-input v-model="form.physicalInfoId" placeholder="请输入体格信息id" />
        </el-form-item>
        <el-form-item label="处方信息id" prop="prescriptionInfoId">
          <el-input v-model="form.prescriptionInfoId" placeholder="请输入处方信息id" />
        </el-form-item>
        <el-form-item label="是否住院" prop="isHospitalized">
          <el-input v-model="form.isHospitalized" placeholder="请输入是否住院" />
        </el-form-item>
        <el-form-item label="床位id" prop="bedId">
          <el-input v-model="form.bedId" placeholder="请输入床位id" />
        </el-form-item>
        <el-form-item label="就诊卡号" prop="membershipCardNumber">
          <el-input v-model="form.membershipCardNumber" placeholder="请输入就诊卡号" />
        </el-form-item>
        <el-form-item label="婚姻状态" prop="maritalStatus">
          <el-radio-group v-model="form.maritalStatus">
            <el-radio
              v-for="dict in patient_marital_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Patient">
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/registration/patient";

const { proxy } = getCurrentInstance();
const { patient_marital_status } = proxy.useDict('patient_marital_status');

const patientList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    patientId: null,
    realName: null,
    membershipLevelId: null,
    medicalRecordId: null,
    phoneNumber: null,
    age: null,
    gender: null,
    idCardNumber: null,
    address: null,
    ethnicId: null,
    occupationId: null,
    physicalInfoId: null,
    prescriptionInfoId: null,
    isHospitalized: null,
    bedId: null,
    receptionStatus: null,
    membershipCardNumber: null,
    maritalStatus: null,
    ext3: null,
    ext4: null,
    ext5: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询患者列表 */
function getList() {
  loading.value = true;
  listPatient(queryParams.value).then(response => {
    patientList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    patientId: null,
    realName: null,
    membershipLevelId: null,
    medicalRecordId: null,
    phoneNumber: null,
    age: null,
    gender: null,
    idCardNumber: null,
    address: null,
    ethnicId: null,
    occupationId: null,
    physicalInfoId: null,
    prescriptionInfoId: null,
    isHospitalized: null,
    bedId: null,
    receptionStatus: null,
    membershipCardNumber: null,
    maritalStatus: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("patientRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.patientId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加患者";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _patientId = row.patientId || ids.value
  getPatient(_patientId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改患者";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["patientRef"].validate(valid => {
    if (valid) {
      if (form.value.patientId != null) {
        updatePatient(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPatient(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _patientIds = row.patientId || ids.value;
  proxy.$modal.confirm('是否确认删除患者编号为"' + _patientIds + '"的数据项？').then(function() {
    return delPatient(_patientIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('registration/patient/export', {
    ...queryParams.value
  }, `patient_${new Date().getTime()}.xlsx`)
}

getList();
</script>
